package com.zn.services.impl;

import com.zn.dao.*;
import com.zn.pojo.MhyMedia;
import com.zn.pojo.UnifiedReportData;
import com.zn.pojo.android.*;
import com.zn.services.MhyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class MhyServiceImpl implements MhyService {

    @Autowired
    private MhyProcessNoInfoMapper processNoMapper;
    @Autowired
    private MhyProcessDataInfoMapper mhyProcessDataInfoMapper;
    @Autowired
    private MhySmokeDetectionMapper mhySmokeDetectionMapper;
    @Autowired
    private MhyMediaMapper mhyMediaMapper;
    @Autowired
    private VehicleInfoMapper vehicleInfoMapper;

    @Override
    public int addProcess(ProcessNo processNo) {
        return processNoMapper.insert(processNo);
    }

    @Override
    public int addProcessBatchList(List<ProcessData> processData) {
        for (int i = 0; i < processData.size(); i++) {
            mhyProcessDataInfoMapper.insert(processData.get(i));
        }
        return processData.size();
    }

    @Override
    public int addResult(SmokeDetection smokeDetection) {
        return mhySmokeDetectionMapper.insert(smokeDetection);
    }

    @Override
    public MhyMedia getMhyMediaByUUId(String uuid) {
        return mhyMediaMapper.selectByUUId(uuid);
    }

    @Override
    public int addMhyMedia(VehicleImage vi) {
        MhyMedia mm = new MhyMedia();
        mm.setUuid(vi.getUuid());
        if(encapsulation(vi,mm) == 0){
            return 0;
        }
        return mhyMediaMapper.insert(mm);
    }

    @Override
    public int addMhyMedia(VehicleResultImage vri) {
        MhyMedia mm = new MhyMedia();
        mm.setProcessNo(vri.getProcessNo());
        if(encapsulation2(vri,mm) == 0){
            return 0;
        }
        return mhyMediaMapper.insert(mm);
    }

    @Override
    public int modifyMhyMedia(VehicleImage vi, Integer mediaId) {
        MhyMedia mm = new MhyMedia();
        mm.setMediaId(mediaId);
        if(encapsulation(vi,mm) == 0){
            return 0;
        }
        return mhyMediaMapper.updateByPrimaryKeySelective(mm);
    }

    @Override
    public int modifyMhyMedia(VehicleResultImage vri, Integer mediaId) {
        MhyMedia mm = new MhyMedia();
        mm.setMediaId(mediaId);
        mm.setProcessNo(vri.getProcessNo());
        if(encapsulation2(vri,mm) == 0){
            return 0;
        }
        return mhyMediaMapper.updateByPrimaryKeySelective(mm);
    }

    @Override
    public int modifyMhyMedia(VehicleResultImage vri, String processNo) {
        MhyMedia mm = new MhyMedia();
        mm.setProcessNo(vri.getProcessNo());
        if(encapsulation2(vri,mm) == 0){
            return 0;
        }
        return mhyMediaMapper.updateByProcessNo(mm);
    }

    @Override
    public SmokeDetection getSmokeDetectionInfoByProcessNo(String processNo) {
        //查询最新的一个 , 可能回有重复数据
        return mhySmokeDetectionMapper.selectByProcessNo(processNo);
    }

    @Override
    public MhyMedia getMhyMediaByProcessNo(String processNo) {
        return mhyMediaMapper.selectByProcessNo(processNo);
    }

    @Override
    public int modifyMhyMediaVideoByProcessNo(String processNo, String videoUrl) {
        return mhyMediaMapper.updateVideoUrl(processNo,videoUrl);
    }

    @Override
    public UnifiedReportData getUnifiedReportData(String uuid) {
        MhyMedia mhyMedia = mhyMediaMapper.selectByUUId(uuid);
        SmokeDetection smokeDetection = mhySmokeDetectionMapper.selectByUUId(uuid);
        ProcessNo processNo =  processNoMapper.selectByUUId(uuid);
        VehicleBean vehicleBean = vehicleInfoMapper.selectByUUId(uuid);
        return new UnifiedReportData(vehicleBean,null,processNo,smokeDetection,mhyMedia);
    }

    @Override
    public int modifySmokeDetectionByKey(SmokeDetection smokeDetection) {
        return mhySmokeDetectionMapper.updateByKey(smokeDetection);
    }

    @Override
    public SmokeDetection getSmokeDetectionInfoByUUId(String uuid) {
        return mhySmokeDetectionMapper.selectByUUId(uuid);
    }

    @Override
    public List<ProcessData> getAllProcessData() {
        return mhyProcessDataInfoMapper.selectAll();
    }

    @Override
    public List<ProcessNo> getAllProcessNo() {

        return processNoMapper.selectAllProcessNo();
    }

    @Override
    public List<SmokeDetection> getAllSmokeDetection() {
        return mhySmokeDetectionMapper.selectAllSmokeDetection();
    }

    @Override
    public List<MhyMedia> getAllMhyMedia() {
        return mhyMediaMapper.selectAllMhyMedia();
    }

    private int encapsulation(VehicleImage vi,MhyMedia mm){
        switch (vi.getFilename()) {
            case "F_PIC1":
                mm.setfPic1(vi.getFileData());
                break;
            case "F_PIC2":
                mm.setfPic2(vi.getFileData());
                break;
            case "F_PIC3":
                mm.setfPic3(vi.getFileData());
                break;
            case "F_PIC4":
                mm.setfPic4(vi.getFileData());
                break;
            case "F_PIC5":
                mm.setfPic5(vi.getFileData());
                break;
            case "F_PIC6":
                mm.setfPic6(vi.getFileData());
                break;
            case "F_PIC7":
                mm.setfPic7(vi.getFileData());
                break;
            case "F_PIC8":
                mm.setfPic8(vi.getFileData());
                break;
            case "F_PIC9":
                mm.setfPic9(vi.getFileData());
                break;
            case "F_FRZSYYZZ":
                mm.setfFrzsyyzz(vi.getFileData());
                break;
            case "F_ET_PIC1":
                mm.setfEtPic1(vi.getFileData());
                break;
            case "F_ET_PIC2":
                mm.setfEtPic2(vi.getFileData());
                break;
            case "F_ET_PIC3":
                mm.setfEtPic3(vi.getFileData());
                break;
            case "F_ET_PIC4":
                mm.setfEtPic4(vi.getFileData());
                break;
            case "F_ET_PIC5":
                mm.setfEtPic5(vi.getFileData());
                break;
            case "F_ET_PIC6":
                mm.setfEtPic6(vi.getFileData());
                break;
            case "F_ET_PIC7":
                mm.setfEtPic7(vi.getFileData());
                break;
            default:
                return 0;
        }
        return 1;
    }
    private int encapsulation2(VehicleResultImage vri,MhyMedia mm){
        switch (vri.getFilename()) {
            case "F_ET_PIC1":
                mm.setfEtPic1(vri.getFileData());
                break;
            case "F_ET_PIC2":
                mm.setfEtPic2(vri.getFileData());
                break;
            case "F_ET_PIC3":
                mm.setfEtPic3(vri.getFileData());
                break;
            case "F_ET_PIC4":
                mm.setfEtPic4(vri.getFileData());
                break;
            case "F_ET_PIC5":
                mm.setfEtPic5(vri.getFileData());
                break;
            case "F_ET_PIC6":
                mm.setfEtPic6(vri.getFileData());
                break;
            case "F_ET_PIC7":
                mm.setfEtPic7(vri.getFileData());
                break;
            default:
                return 0;
        }
        return 1;
    }

}
